﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace InternshipManagement.Models
{
    [MetadataType(typeof(Announcement_Validation))]
    public partial class Announcement { }

    [OneMustBeTrueAttribute("IsStudentReceiving", "IsCompanyReceiving", "IsSecretaryReceiving")]
    public class Announcement_Validation
    {
        [Required]
        [DisplayName("Title")]
        public string Title { get; set; }

        [Required]
        [DisplayName("Message")]
        public string Message { get; set; }

        [DisplayName("Students")]
        public bool IsStudentReceiving { get; set; }
        [DisplayName("Companies")]
        public bool IsCompanyReceiving { get; set; }
        [DisplayName("Secretaries")]
        public bool IsSecretaryReceiving { get; set; }

    }


    //[OneMustBeTrueAttribute("IsStudentReceiving", "IsCompanyReceiving", "IsSecretaryReceiving")]
    //public class AnnouncementModel
    //{
    //    [Required]
    //    [DisplayName("Title")]
    //    public string Title { get; set; }

    //    [Required]
    //    [DisplayName("Message")]
    //    public string Message { get; set; }

    //    [DisplayName("Students")]
    //    public bool IsStudentReceiving { get; set; }
    //    [DisplayName("Companies")]
    //    public bool IsCompanyReceiving { get; set; }
    //    [DisplayName("Secretaries")]
    //    public bool IsSecretaryReceiving { get; set; }
    //}

    #region Validation

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class OneMustBeTrueAttribute : ValidationAttribute
    {
        private const string _defaultErrorMessage = "At least one of the '{0}','{1}' or '{2}' fileds must be selected.";
        private readonly object _typeId = new object();

        public OneMustBeTrueAttribute(string value1, string value2, string value3)
            : base(_defaultErrorMessage)
        {
            Value1 = value1;
            Value2 = value2;
            Value3 = value3;
        }

        public string Value1 { get; private set; }
        public string Value2 { get; private set; }
        public string Value3 { get; private set; }

        public override object TypeId
        {
            get
            {
                return _typeId;
            }
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(ErrorMessageString,
                Value1, Value2, Value3);
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object value1 = properties.Find(Value1, true).GetValue(value);
            object value2 = properties.Find(Value2, true).GetValue(value);
            object value3 = properties.Find(Value3, true).GetValue(value);
            return value1.Equals(true) || value2.Equals(true) || value3.Equals(true);
        }
    }

    #endregion
}